package com.flute.icrawler.app.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.jobbox.common.CloudConstants;
import com.flute.haflute.jobbox.common.CloudContext;
import com.flute.haflute.tools.PropertyUtils;
import com.flute.haflute.web.SimpleHttpServer;

/**
 * 启动JettyServer的web服务
 * 
 * @author Jazz
 * 
 */
public class JettyServer {
	private static Logger logger = LoggerFactory.getLogger(JettyServer.class);

	final private static Collection<String> ALL_INTERFACES = Collections
			.unmodifiableList(Arrays.asList(new String[] { "0.0.0.0" }));

	private static String adminUsername;

	private static String adminPassword;

	public static final String ROOT_CONTEXT = "/";

	public static String ADMIN = "admin";

	private static SimpleHttpServer httpServer = null;

	/**
	 * 默认的Web端口.
	 */
	public static final int DEFAULT_PORT = 8080;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		startJettyServer();

	}

	public static void startJettyServer() throws IOException, Exception {
		TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
		containerInitialization();
		String adminLoginPassword = PropertyUtils
				.getPropertyOrNull(CloudConstants.CMD_LINE_ADMIN_PROPERTIES);
		String log = startEmbeddedWebserver(ALL_INTERFACES, DEFAULT_PORT,
				adminLoginPassword);
		logger.info(log);
	}

	/**
	 * 启动嵌入的Web服务器
	 * 
	 * @param hosts
	 *            主机列表
	 * @param port
	 *            端口
	 * @param adminLoginPassword
	 *            管理员登录密码
	 * @throws Exception
	 */
	protected static String startEmbeddedWebserver(Collection<String> hosts,
			int port, String adminLoginPassword) throws Exception {
		adminUsername = adminLoginPassword.substring(0,
				adminLoginPassword.indexOf(":"));
		adminPassword = adminLoginPassword.substring(adminLoginPassword
				.indexOf(":") + 1);
		httpServer = new SimpleHttpServer("admin", ROOT_CONTEXT, hosts, port,
				false);

		final String ROLE = ADMIN;
		httpServer.setAuthentication(ROLE, ROOT_CONTEXT, adminUsername,
				adminPassword, ROLE);
		httpServer.startServer();
		StringBuffer buffer = new StringBuffer();
		for (String host : httpServer.getHosts()) {
			buffer.append("\nWeb console is at: http://");
			buffer.append(host).append(':').append(port);
		}
		buffer.append("\nWeb console login and password: " + adminUsername
				+ "/" + adminPassword);
		return buffer.toString();
	}

	/**
	 * 返回读入的Properties的输入流
	 * 
	 * @throws IOException
	 */
	protected static InputStream getPropertiesInputStream() throws IOException {
		InputStream inputsteam = new FileInputStream(CloudContext.getRootDir()
				+ CloudConstants.JETTY_PROPERTIES_FILENAME);
		Properties properties = new Properties();
		Iterator iterator = properties.keySet().iterator();
		List<String> list = new ArrayList<String>();
		String optionString = "";

		while (iterator.hasNext()) {

			String pattenName = (String) iterator.next();
			list.add(pattenName);

		}

		Collections.sort(list);
		for (int i = 0; i < list.size(); i++) {
			String pattenName = list.get(i);
			String pattenValue = properties.getProperty(pattenName);
			optionString = optionString + "<option value= '" + pattenValue
					+ "'>" + pattenName + "</option>";
		}

		return inputsteam;
	}

	/**
	 * 将读入的Properties属性拷贝到系统的Property中
	 * 
	 * @param key
	 *            系统Property的key
	 * @param value
	 *            系统Property的value
	 */
	protected static void copyToSystemProperty(String key, String value) {
		if (System.getProperty(key) == null
				|| System.getProperty(key).length() == 0) {
			System.setProperty(key, value);
		}
	}

	/**
	 * 加载Properties文件
	 * 
	 * @throws IOException
	 */
	protected static Properties loadProperties() throws IOException {
		Properties properties = new Properties();
		properties.load(getPropertiesInputStream());

		for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
			String key = ((String) e.nextElement()).trim();
			String value = properties.getProperty(key).trim();
			copyToSystemProperty(key, value);
		}

		return properties;
	}

	/**
	 * 容器初始化
	 * 
	 * @throws IOException
	 */
	protected static void containerInitialization() throws IOException {
		loadProperties();
	}

}
